package gogogo.liwushuo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.CgleftBottomImageItemBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.CommonAdapter;
import gogogo.liwushuo.util.MyHolder;
import gogogo.liwushuo.util.OKHttpUtils;

/**
 * Created by Godman_Kai on 2016/7/23.
 */
//R.layout.item_home_recycler
public class CgleftBottomImageItem extends AppCompatActivity {
    @InjectView(R.id.tv_cgleft_bottom_imageitem_toolbar_title)
    TextView tvCgleftBottomImageitemToolbarTitle;
    @InjectView(R.id.rv_cgleft_bottom_imageitem)
    RecyclerView rvCgleftBottomImageitem;
    private int mId;
    private CommonAdapter<CgleftBottomImageItemBean.DataBean.ItemsBean> mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cgleftbottomimageitem);
        ButterKnife.inject(this);

        loadDatas();
    }

    private void loadDatas() {

        Intent intent = getIntent();
        mId = intent.getIntExtra("id",0);
        OKHttpUtils.newInstance(this).getAsyncMainData(Resource.getCategoryBottomIntentUrl(mId), new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                CgleftBottomImageItemBean mBean = new Gson().fromJson(result, CgleftBottomImageItemBean.class);
                mAdapter = new CommonAdapter<CgleftBottomImageItemBean.DataBean.ItemsBean>
                        (CgleftBottomImageItem.this,mBean.getData().getItems(),R.layout.item_home_recycler) {
                    @Override
                    public void bindData(MyHolder holder, int position, CgleftBottomImageItemBean.DataBean.ItemsBean bean) {
                        holder.setText(bean.getColumn().getCategory(),R.id.tv_category_recycler)
                                .setText(bean.getColumn().getTitle(),R.id.tv_title_recycler)
                                .displayImage(bean.getAuthor().getAvatar_url(),R.id.iv_icon_recycler)
                                .setText(bean.getAuthor().getNickname(),R.id.tv_author_recycler)
                                .displayImage(bean.getCover_image_url(),R.id.iv_image_recycler)
                                .setText(bean.getTitle(),R.id.tv_digest_recycler)
                                .setText(Integer.toString(bean.getLikes_count()),R.id.tv_count_recycler);
                    }
                };
                rvCgleftBottomImageitem.setAdapter(mAdapter);
                rvCgleftBottomImageitem.setLayoutManager(new LinearLayoutManager(CgleftBottomImageItem.this,LinearLayoutManager.VERTICAL,false));
                tvCgleftBottomImageitemToolbarTitle.setText(mBean.getData().getItems().get(0).getColumn().getCategory());
                mAdapter.notifyDataSetChanged();

            }
        });
    }

    @OnClick(R.id.iv_back_cgleft_bottom_imageitem)
    public void onClick() {
        finish();
    }
}
